package j2025.j05;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 猫大仙
 */
public class j0514 {
    int left,right,nn;
    List<String> ret;
    StringBuilder path;
    public List<String> generateParenthesis(int n) {
        left = 0;right = 0;
        nn = n;
        ret = new ArrayList<>();
        path = new StringBuilder();
        dfs();
        return ret;
    }
    public void dfs(){
        if(right==nn){
            ret.add(path.toString());
            return;
        }
        if(left<nn){
            path.append("(");
            left++;
            dfs();
            path.deleteCharAt(path.length()-1);
            left--;
        }
        if(right<left){
            path.append(")");
            right++;
            dfs();
            path.deleteCharAt(path.length()-1);
            right--;
        }
    }
}
